package com.gitee.feizns.quickstart.ding.talk.api.top.api.v2.department;

import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiV2DepartmentListparentbyuserRequest;
import com.dingtalk.api.response.OapiV2DepartmentListparentbyuserResponse;
import com.dingtalk.api.response.OapiV2DepartmentListsubResponse;
import com.gitee.feizns.quickstart.ding.talk.utils.Utils;
import lombok.SneakyThrows;

import java.util.List;

/**
 * 获取指定用户的所有父部门列表（/topapi/v2/department/listparentbyuser）
 * 接口路径：<a href="https://open.dingtalk.com/document/orgapp/queries-the-list-of-all-parent-departments-of-a-user">获取指定用户的所有父部门列表（/topapi/v2/department/listparentbyuser）</a>
 * @link <a href="https://open.dingtalk.com/document/orgapp/queries-the-list-of-all-parent-departments-of-a-user">服务端API 通讯录管理 部门管理 获取指定用户的所有父部门列表</a>
 * @author feizns
 * @since 2024/6/29
 */
public class ListParentByUser {

    /**
     * 获取指定用户的所有父部门列表
     * @param accessToken 访问令牌
     * @param userid 用户 ID
     * @return {@link List }<{@link OapiV2DepartmentListsubResponse.DeptBaseResponse }>
     */
    @SneakyThrows
    public static List<OapiV2DepartmentListparentbyuserResponse.DeptParentResponse> execute(String accessToken, String userid) {
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/department/listparentbydept");
        OapiV2DepartmentListparentbyuserRequest req = new OapiV2DepartmentListparentbyuserRequest();
        req.setUserid(userid);
        OapiV2DepartmentListparentbyuserResponse rsp = client.execute(req, accessToken);
        return Utils.ifErrorThrow(rsp).getResult().getParentList();
    }

}
